
use ../Intermediate/borrower_chars, clear 
kountry Country, from(other) geo(marc)
gen Region_borrower = GEO 
tab Region_borrower, sort
replace Region_borrower = "Asia" if inlist(Region_borrower,"West Indies", "Indian Ocean","Pacific Ocean")
tab Region_borrower, sort 
drop Continent Region Country GEO
rename NAMES_STD Country_borrower
tempfile borrower_chars 
save `borrower_chars'

use ../Intermediate/transaction_clean, clear
joinby transaction_id using ../Intermediate/transaction_lender_clean
order transaction_id borrower_id BankID 
drop if missing(BankID)
gen year=yofd(date_trans)
drop if amount_trans_lender_usdm < 0
collapse (sum) amount_trans_lender_usdm, by(BankID borrower_id year)

keep if year>2008
drop if year == . | year > 2021

*merge m:1 BankID using ../Intermediate/bank_cx, keepusing(Region_bank Country_bank) keep(3) nogen
tempfile obs 
save `obs'

//make panel at bank borrower year level
keep BankID borrower_id
duplicates drop
expand 13
bys borrower_id BankID: gen year = 2008+_n

merge 1:1 borrower_id BankID year using `obs'
rename amount_trans_lender_usdm borr_fbt
gen post = year > 2014
replace borr_fbt = 0 if missing(borr_fbt)
gen has_borr_fbt = _merge == 3
drop _merge 

merge m:1 BankID year using ../Intermediate/bank_ban_strength_yearly
egen ban_intensity_new1_max = max(ban_intensity_new1), by(BankID)
drop if _merge == 2
drop _merge

merge m:1 borrower_id year using ../Intermediate/analysis_panel_firmyear, keepusing(share Country country_i coal_industry_mining coal_industry_power median_assets_mean) keep(1 3) nogen
merge m:1 borrower_id using `borrower_chars', keepusing(Country_borrower Region_borrower) keep(1 3) nogen



replace ban_intensity_new1 = 0 if missing(ban_intensity_new1)
gen log_1p_borr_fbt = log(1+borr_fbt)

gen highshare = share > 0.5562 if ~missing(share)


*Labels
label var ban_intensity_new1 "$\text{Exit Policy Strength}_{b,t}$"
label define medcoal 0 "$\text{Low Coal Share}_{f}$" 1 "$\text{High Coal Share}_{f}$", replace
label values highshare medcoal
label values median_assets_mean medassets

save "../Intermediate/bank_supply_chain_regressions", replace
